﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Reflection;
using System.Data;

namespace Esint.Common
{
    public static class ExtendFuncs
    {
        public static bool IsNullOrCountZero<T>(this IList<T> list)
        {
            if (list != null && list.Count > 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public static bool IsNotNullOrCountZero<T>(this IList<T> list)
        {
            if (list != null && list.Count > 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public static bool IsNotNull(this object obj)
        {
            if (obj != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool IsNull(this object obj)
        {
            if (obj == null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 判断字符串是否空串或者null
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this string str)
        {
            if (string.IsNullOrWhiteSpace(str))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 判断字符串是否非空串或者null
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNotNullOrEmpty(this string str)
        {
            if (!string.IsNullOrWhiteSpace(str))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 判断字符串不为null，但是空值
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsEmptyButNotNull(this string str)
        {
            if (str != null && str.Length == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static List<T> ToList<T>(this JArray jArray)
        {
            List<T> flagList = new List<T>();
            for (int i = 0; i < jArray.Count; i++)
            {
                flagList.Add(jArray[i].Value<T>());
            }
            return flagList;
        }

        /// <summary>
        /// 判断datareader中是否存在某列
        /// </summary>
        /// <param name="dr"></param>
        /// <param name="columnName"></param>
        /// <returns></returns>
        public static bool ColumnExists(this IDataReader dr, string columnName)
        {
            dr.GetSchemaTable().DefaultView.RowFilter = "ColumnName= '" + columnName + "'";
            return (dr.GetSchemaTable().DefaultView.Count > 0);
        }

        /// <summary>
        /// 随机去集合中的若干条数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="randomNumber"></param>
        /// <returns></returns>
        public static IList<T> GetRandomObject<T>(this IList<T> list, int randomNumber)
        {
            if (randomNumber > list.Count)
            {
                throw new Exception("随机数个数不能大于集合总数量");
            }
            if (randomNumber == list.Count)
            {
                return list;
            }

            List<int> containsList = new List<int>();
            while (containsList.Count < randomNumber)
            {
                int n = CommonExtend.CustomRandom(0, list.Count);
                if (containsList.Contains(n))
                {
                    continue;
                }
                else
                {
                    containsList.Add(n);
                }
            }

            List<T> listRet = new List<T>();
            for (int i = 0; i < containsList.Count; i++)
            {
                var listItemIndex = containsList[i];
                var item = list[listItemIndex];
                listRet.Add(item);
            }

            return listRet;
        }
        
    }
}
